//
//  ShenQingJiLvViewController.swift
//  demo
//
//  Created by HAip on 2020/2/19.
//  Copyright © 2020 CodeYu. All rights reserved.
//

import UIKit

class ShenQingJiLvViewController: UIViewController {
    
    // MARK: UI
    
    lazy var nav = HZCustomNavigationBar.customNavigationBar().then { (bar) in
         bar.barBackgroundColor = UIColor(hex: "fbfbfb")
         bar.shadowImageHidden = false
         /* 闭包 相当于block
          { (参数) -> (返回值) in
          
          }
          */
         bar.hz.setItemsToLeft([HZNavigationBarItem.create(normalImage: "back", barItemClickHandler: { (item) in
             self.navigationController?.popViewController(animated: true)
         }), HZNavigationBarItem.create(normalTitle: "申请记录", barItemClickHandler: {
             (item) in
         })])
     }
    
    
    
    lazy var filterView = UIView().then{
        
        let allBtn = UIButton().then {
            $0.setTitle("全部", for: .normal)
            $0.setTitleColor(UIColor.red, for: .selected)
            $0.setTitleColor(UIColor.gray, for: .normal)
            $0.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            $0.actionBlock = { (sender) in
                self.filterChange(sender)
            }
            $0.tag = 1000;
        }
    

        let passBtn = UIButton().then {
            $0.setTitle("已通过", for: .normal)
            $0.setTitleColor(UIColor.red, for: .selected)
            $0.setTitleColor(UIColor.gray, for: .normal)
            $0.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            $0.actionBlock = { (sender) in
                self.filterChange(sender)
            }
            $0.tag = 1001
        }
        let noPassBtn = UIButton().then{
            $0.setTitle("未通过", for: .normal)
            $0.setTitleColor(UIColor.red, for: .selected)
            $0.setTitleColor(UIColor.gray, for: .normal)
            $0.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            $0.actionBlock = { (sender) in
                self.filterChange(sender)
            }
            $0.tag = 1002

        }
        
        let resultLab = UILabel().then{
            $0.textColor = UIColor.gray
            $0.font = UIFont.systemFont(ofSize:15)
            $0.text = "共4条记录"
            $0.tag = 1003
        }
        
        $0.addSubview(allBtn)
        $0.addSubview(passBtn)
        $0.addSubview(noPassBtn)
        $0.addSubview(resultLab)
        
        allBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.bottom.equalToSuperview()
        }
        passBtn.snp.makeConstraints { (make) in
            make.left.equalTo(allBtn.snp.right).offset(20)
            make.top.bottom.equalToSuperview()
        }
        noPassBtn.snp.makeConstraints { (make) in
            make.left.equalTo(passBtn.snp.right).offset(20)
            make.top.bottom.equalToSuperview()
        }
        resultLab.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
        }
        
    }
    
    var pageVC = PageViewController()
    
    
    var selectedBtn : UIButton?

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: "f4f4f4")
        
        view.addSubview(nav)
        
        view.addSubview(filterView)
        filterView.snp.makeConstraints { (make) in
            make.top.equalTo(nav.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(40)
        }
        
        view.addSubview(pageVC.view)
        self.addChild(pageVC)
        pageVC.view.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(filterView.snp.bottom)
        }
        pageVC.pageScrooCallBack = {[weak self] (index) in
            self?.filterChange(self?.filterView.viewWithTag(1000 + index) as! UIButton)
        }
        pageVC.isAllowPageScroll = true
        
        let typeAry = ["all","pass","nopass"]
        var controllrs : [UIViewController] = []
        for type in typeAry {
          let vc =  ShenQingJiLvListController()
            vc.type = type
          controllrs.append(vc)
        }
        
        pageVC.updateDataSource(controllers: controllrs)
        filterChange(filterView.viewWithTag(1000) as! UIButton)
        
        
        // Do any additional setup after loading the view.
    }
    
    
    func filterChange(_ sender: UIButton){
        selectedBtn?.isSelected = false
        selectedBtn = sender
        selectedBtn?.isSelected = true
        pageVC.selectViewControllerAtIndex(index: selectedBtn!.tag - 1000, animated: true)
    }

}

